﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using Utils.Adapter.Flows.Models;

namespace Utils.Adapter.Flows
{
    public class FlowAdapter : AdapterBase
    {

        private readonly ConfigService configService;

        public override string BaseUrl => "https://jmexpresv2.market.alicloudapi.com";

        public FlowAdapter(ConfigService configService)
        {
            this.configService = configService;
        }

        protected override HttpRequestMessage Filter(HttpRequestMessage httpRequest)
        {
            // 认证代号
            httpRequest.Headers.Add("Authorization", "APPCODE " + configService.GetValue("SysConfig", "FlowCode"));
            httpRequest.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            return base.Filter(httpRequest);
        }
         
        /// <summary>
        ///  查询物流轨迹
        /// </summary>
        /// <param name="number"></param>
        /// <param name="mobile"></param>
        public async Task<FlowInfo> QueryFlow(string number, string mobile)
        {
            var result = await httpClient.PostAsync($"/express/query-v2", new JsonContent(new { number, mobile })).ReadAsJTokenAsync();
            return result["data"].ToObject<FlowInfo>(JsonContent.DefaultJsonSerializer);
        }


    }
}
